﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Xml;
using System.Text;

public partial class single_job : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        resolve_btn.Attributes.Add("onclick", "return confirm('Are you sure you want to resolve this job? This cannot be undone');");
    }

    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
    }

    protected void update_p2()
    {
        int team_numb = Int32.Parse(DetailsView1.Rows[4].Cells[1].Text);
        DateTime dt_now = DateTime.Parse("#" + DateTime.Now.ToShortDateString() + "#");
        DateTime dt_created = DateTime.Parse(DetailsView1.Rows[3].Cells[1].Text);
        TimeSpan time_elapsed = dt_now - dt_created;
        int days_int = time_elapsed.Days;
        int days_allowed = 6;
        bool resting = false;

        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";
        OleDbConnection db_conn = new OleDbConnection(connString);
        OleDbCommand updateCom = new OleDbCommand();
        //OleDbCommand updateCom = new OleDbCommand("UPDATE TEAMS SET [CURRENTLY WORKING] = ?, [Days Worked Recently] = ?, [Resting] = ?, [Date last rested] = ? WHERE ([TEAM_ID] = ?)", db_conn);

        if (days_int > days_allowed) // If it is greater than the days allowed this will set the worker to resting and re-set the days worked recently
        {
            updateCom.CommandText = "UPDATE TEAMS SET [CURRENTLY WORKING] = ?, [Days Worked Recently] = ?, [Resting] = ?, [Date last rested] = ? WHERE ([TEAM_ID] = ?)";
            updateCom.Connection = db_conn;
            days_int = 0;
            resting = true;
            updateCom.Parameters.Add("CURRENTLY WORKING", false);//always the case -- remains the same regardless of the conditions
            updateCom.Parameters.Add("Days Worked Recently", days_int);
            updateCom.Parameters.Add("Resting", resting);
            updateCom.Parameters.Add("Date last rested", dt_now);
            updateCom.Parameters.Add("TEAM_ID", team_numb);
        }
        else // If it isn't then it just adds it on and away you go ready for another job.
        {
            //NEED TO CHECK THE VALUE OF DAYS WORKING AND ADD A VALUE HERE. 

            updateCom.CommandText = "UPDATE TEAMS SET [CURRENTLY WORKING] = ?, [Days Worked Recently] = ?, [Resting] = ? WHERE ([TEAM_ID] = ?)";
            updateCom.Connection = db_conn;
            updateCom.Parameters.Add("CURRENTLY WORKING", false);
            updateCom.Parameters.Add("Days Worked Recently", days_int);
            updateCom.Parameters.Add("Resting", resting);
            updateCom.Parameters.Add("TEAM_ID", team_numb);
        }

        db_conn.Open();
        updateCom.ExecuteNonQuery(); //executes the paramaters added depending on the conditions of the if statement
        db_conn.Close();
        

    }



    protected void resolve_btn_Click(object sender, EventArgs e)
    {
        update_p1();
        update_p2();
        Response.Redirect("~/Department Administrator Page.aspx?&state_ID=" + Int32.Parse(Request.QueryString["state_ID"]) + "&dept=" + Request.QueryString["dept"]);
    }


    protected void update_p1()
    {
        int job_numb = Int32.Parse(DetailsView1.Rows[1].Cells[1].Text);
        DateTime dt_now = DateTime.Parse("#" + DateTime.Now.ToShortDateString() + "#");
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";
        OleDbConnection db_conn = new OleDbConnection(connString);
        OleDbCommand updateCom = new OleDbCommand("UPDATE Jobs SET [Job Completed?] = ?, [Date Resolved] = ? WHERE (Job_ID = ?)", db_conn);
        updateCom.Parameters.Add("Job Completed?", true);
        updateCom.Parameters.Add("Date Resolved", dt_now);
        updateCom.Parameters.Add("Job_ID", job_numb);
        db_conn.Open();
        updateCom.ExecuteNonQuery(); //executes the paramaters added depending on the conditions of the if statement
        db_conn.Close();
    }

}